package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetMealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     */
    @Transactional//涉及两个表，需要事务控制
    public void saveWithDish(SetmealDTO setmealDTO) {
        //保存套餐基本信息，操作setmeal，执行insert操作
        //1.属性拷贝
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //2.调用mapper执行插入操作
        setmealMapper.save(setmeal);
        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
        //1.通过主键返回，获取套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //2.调用mapper执行批量插入操作
        setmealDishMapper.saveWithDish(setmealDishes);
    }

    /**
     * 套餐分页查询
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.设置分页条件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //2.调用mapper执行左外联查询
        List<SetmealVO> SetmealVOlist = setmealMapper.findAll(setmealPageQueryDTO);
        //3.把VO封装成page对象
        Page setmealVOlist = (Page<SetmealVO>) SetmealVOlist;
        //4.封装结果并返回
        return new PageResult(setmealVOlist.getTotal(),setmealVOlist.getResult());
    }

    /**
     * 批量删除套餐
     */
    public void deleteBatch(Long[] ids) {
        setmealMapper.deleteBatch(ids);
    }

    /**
     * 修改套餐
     * 1. 修改套餐基本信息: setmeal
     * 2. 修改套餐菜品关系表: setmeal_dish
     */
    public void updateWithDish(SetmealDTO setmealDTO) {
       //1. 修改套餐基本信息: setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

       //2. 修改套餐菜品关系表: setmeal_dish
        Long setmealId = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //更新操作先删除套餐菜品关系表: setmeal_dish
        setmealDishMapper.deleteBySetmealId(setmealId);
        //再插入操作
        setmealDishMapper.saveWithDish(setmealDishes);

    }

    /**
     * 起售停售套餐
     */
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * 1. 查询套餐基本信息，从setmeal表查询
     * 2. 查询当前套餐对应的菜品信息，从setmeal_dish表查询
     */
    public SetmealDTO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //根据套餐id查询套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
